//
//  XiaoximenResultDetailViewController.m
//  Xiaoximen_iOS
//
//  Created by Wang Zhongyu on 12-7-20.
//  Copyright (c) 2012年 Peking University. All rights reserved.
//

#import "XiaoximenResultDetailViewController.h"
#import "ASIHTTPRequest.h"
#import "XiaoximenAppDelegate.h"

@interface XiaoximenResultDetailViewController ()

@end

@implementation XiaoximenResultDetailViewController
@synthesize placeTableView;
@synthesize timeTableView;
@synthesize showAll;
@synthesize party;
@synthesize person;
@synthesize placeImages;
@synthesize timeImages;
@synthesize placeYess;
@synthesize timeYess;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
	placeImages = [[NSMutableArray alloc] init];
	for (int i = 0; i < party.places.count; i++) {
		UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 200, 43)];
		imageView.image = [UIImage imageNamed:@"textfield"];
		[placeImages addObject:imageView];
	}
	
	timeImages = [[NSMutableArray alloc] init];
	for (int i = 0; i < party.timeIntervals.count; i++) {
		UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 200, 43)];
		imageView.image = [UIImage imageNamed:@"textfield"];
		[timeImages addObject:imageView];
	}
	
	placeYess = [[NSMutableArray alloc] init];
	for (int i = 0; i < party.places.count; i++) {
		UIImageView *yes = [[UIImageView alloc] initWithFrame:CGRectMake(200, 12, 20, 20)];
		yes.image = [UIImage imageNamed:@"y"];
		[placeYess addObject:yes];
	}
	
	timeYess = [[NSMutableArray alloc] init];
	for (int i = 0; i < party.timeIntervals.count; i++) {
		UIImageView *yes = [[UIImageView alloc] initWithFrame:CGRectMake(200, 12, 20, 20)];
		yes.image = [UIImage imageNamed:@"y"];
		[timeYess addObject:yes];
	}
	
	if (showAll) self.title = @"投票情况";
	else self.title = [NSString stringWithFormat:@"投票者: %@", [person name]];
}

- (void)viewDidUnload
{
    [self setPlaceTableView:nil];
    [self setTimeTableView:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
	if ([tableView isEqual:placeTableView]) return party.places.count;
	else return party.timeIntervals.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
	UITableViewCell *cell;
	if ([tableView isEqual:placeTableView]) {
		static NSString *CellIdentifier = @"placeIdentifier";
		cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
		
		if (!showAll && [[person.placeVotes valueForKey:[NSString stringWithFormat:@"%d", [[party.places objectAtIndex:indexPath.row] ID]]] isEqualToString:@"YES"]) {
			UIImageView * imageView = [placeImages objectAtIndex:indexPath.row];
			imageView.image = [UIImage imageNamed:@"textfield"];
		}
		[cell addSubview:[placeImages objectAtIndex:indexPath.row]];
		if (!showAll && [[person.placeVotes valueForKey:[NSString stringWithFormat:@"%d", [[party.places objectAtIndex:indexPath.row] ID]]] isEqualToString:@"YES"]) [cell addSubview:[placeYess objectAtIndex:indexPath.row]];
		
		UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20, 9, 140, 25)];
		label.text = [[party.places objectAtIndex:indexPath.row] placeName];
		label.font = [UIFont boldSystemFontOfSize:17];
		label.numberOfLines = 0;
		label.backgroundColor = [UIColor colorWithWhite:1.0 alpha:0.0];
		[cell addSubview:label];
		
		if (showAll) {
			UILabel *resultLabel = [[UILabel alloc] initWithFrame:CGRectMake(150, 11, 80, 21)];
			resultLabel.text = [NSString stringWithFormat:@"%d/%d", [[party.places objectAtIndex:indexPath.row] yesVote], [[party.places objectAtIndex:indexPath.row] totalVote]];
			resultLabel.font = [UIFont boldSystemFontOfSize:17];
			resultLabel.textColor = _ColorResult;
			resultLabel.numberOfLines = 0;
			resultLabel.textAlignment = UITextAlignmentRight;
			resultLabel.backgroundColor = [UIColor colorWithWhite:1.0 alpha:0.0];
			[cell addSubview:resultLabel];
		}
		
	} else {
		static NSString *CellIdentifier = @"timeIdentifier";
		cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
		
		if (!showAll && [[person.timeVotes valueForKey:[NSString stringWithFormat:@"%d", [[party.timeIntervals objectAtIndex:indexPath.row] ID]]] isEqualToString:@"YES"]) {
			UIImageView * imageView = [timeImages objectAtIndex:indexPath.row];
			imageView.image = [UIImage imageNamed:@"textfield"];
		}
		[cell addSubview:[timeImages objectAtIndex:indexPath.row]];
		if (!showAll && [[person.timeVotes valueForKey:[NSString stringWithFormat:@"%d", [[party.timeIntervals objectAtIndex:indexPath.row] ID]]] isEqualToString:@"YES"]) [cell addSubview:[timeYess objectAtIndex:indexPath.row]];
		
		UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20, 6, 180, 15)];
		label.text = [[party.timeIntervals objectAtIndex:indexPath.row] displayDay];
		label.font = [UIFont boldSystemFontOfSize:15];
		label.numberOfLines = 0;
		label.backgroundColor = [UIColor colorWithWhite:1.0 alpha:0.0];
		[cell addSubview:label];
		
		UILabel *label2 = [[UILabel alloc] initWithFrame:CGRectMake(20, 21, 180, 15)];
		label2.text = [[party.timeIntervals objectAtIndex:indexPath.row] displayTimeInterval];
		label2.font = [UIFont boldSystemFontOfSize:15];
		label2.numberOfLines = 0;
		label2.backgroundColor = [UIColor colorWithWhite:1.0 alpha:0.0];
		[cell addSubview:label2];
		
		if (showAll) {
			UILabel *resultLabel = [[UILabel alloc] initWithFrame:CGRectMake(150, 11, 80, 21)];
			resultLabel.text = [NSString stringWithFormat:@"%d/%d", [[party.timeIntervals objectAtIndex:indexPath.row] yesVote], [[party.timeIntervals objectAtIndex:indexPath.row] totalVote]];
			resultLabel.font = [UIFont boldSystemFontOfSize:17];
			resultLabel.textColor = _ColorResult;
			resultLabel.numberOfLines = 0;
			resultLabel.textAlignment = UITextAlignmentRight;
			resultLabel.backgroundColor = [UIColor colorWithWhite:1.0 alpha:0.0];
			[cell addSubview:resultLabel];
		}
	}
	
    return cell;
}

/*
 // Override to support conditional editing of the table view.
 - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
 {
 // Return NO if you do not want the specified item to be editable.
 return YES;
 }
 */

/*
 // Override to support editing the table view.
 - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
 {
 if (editingStyle == UITableViewCellEditingStyleDelete) {
 // Delete the row from the data source
 [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
 }   
 else if (editingStyle == UITableViewCellEditingStyleInsert) {
 // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
 }   
 }
 */

/*
 // Override to support rearranging the table view.
 - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
 {
 }
 */

/*
 // Override to support conditional rearranging of the table view.
 - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
 {
 // Return NO if you do not want the item to be re-orderable.
 return YES;
 }
 */

#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
	/*
	UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
	if ([tableView isEqual:placeTableView]) {
		if ([[[placeYess objectAtIndex:indexPath.row] superview] isEqual:cell]) {
			[[placeYess objectAtIndex:indexPath.row] removeFromSuperview];
			UIImageView *image = [placeImages objectAtIndex:indexPath.row];
			image.image = [UIImage imageNamed:@"text"];
		} else {
			[cell addSubview:[placeYess objectAtIndex:indexPath.row]];
			UIImageView *image = [placeImages objectAtIndex:indexPath.row];
			image.image = [UIImage imageNamed:@"textfield"];
		}
	} else {
		if ([[[timeYess objectAtIndex:indexPath.row] superview] isEqual:cell]) {
			[[timeYess objectAtIndex:indexPath.row] removeFromSuperview];
			UIImageView *image = [timeImages objectAtIndex:indexPath.row];
			image.image = [UIImage imageNamed:@"text"];
		} else {
			[cell addSubview:[timeYess objectAtIndex:indexPath.row]];
			UIImageView *image = [timeImages objectAtIndex:indexPath.row];
			image.image = [UIImage imageNamed:@"textfield"];
		}
	}*/
}


- (void)requestFinished:(ASIHTTPRequest *)request
{
	NSData *responseData = [request responseData];
	id res = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONWritingPrettyPrinted error:nil];
	NSLog(@"%@", res);
	
	[self performSegueWithIdentifier:@"CompleteVoteSegue" sender:self];
}

- (void)requestFailed:(ASIHTTPRequest *)request
{
	NSError *error = [request error];
	NSLog(@"%@", error);
	UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"网络不太好的样子" message:nil delegate:nil cancelButtonTitle:@"再联网试试^_^" otherButtonTitles:nil];
	[alert show];
}
@end
